package tv.youxing.android.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.kymjs.rxvolley.RxVolley;
import com.kymjs.rxvolley.client.HttpCallback;
import com.kymjs.rxvolley.client.HttpParams;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import tv.youxing.android.R;
import tv.youxing.android.custom.CustomToast;
import tv.youxing.android.entity.DianZhanModel;
import tv.youxing.android.entity.ViewPlayerPingLunModel;
import tv.youxing.android.ui.DynamicDetailsActivity_new;
import tv.youxing.android.ui.LoginActivity;
import tv.youxing.android.utils.AppConst;
import tv.youxing.android.utils.GsonUtils;
import tv.youxing.android.utils.PicassoUtils;
import tv.youxing.android.utils.ShareUtils;
import tv.youxing.android.utils.StaticClass;

/**
 * Created by Administrator on 2017/6/22.
 * 评论列表数据
 */

public class VideoPingLunAdapter extends BaseAdapter {


    Context mContext;
    List<ViewPlayerPingLunModel.ResultBean.DataBean> mPinglunListData = new ArrayList<>();//评论列表数据
    private LayoutInflater layoutInflater;


    public VideoPingLunAdapter(Context context, List<ViewPlayerPingLunModel.ResultBean.DataBean> pinglunListData) {
        mContext = context;
        mPinglunListData = pinglunListData;
        this.layoutInflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
    }


    @Override
    public int getCount() {
        return this.mPinglunListData == null ? 0 : mPinglunListData.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        Holder viewHolder = null;
        if (viewHolder == null) {
            viewHolder = new Holder();
            convertView = layoutInflater.inflate(R.layout.video_pinglun_item, null);
            viewHolder.iv_head = (ImageView) convertView.findViewById(R.id.ivIcon);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tvName);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tvTime);
            viewHolder.iv_dianzhan = (ImageView) convertView.findViewById(R.id.ivPraise);
            viewHolder.tv_content = (TextView) convertView.findViewById(R.id.tvContent);
            viewHolder.addOne = (TextView) convertView.findViewById(R.id.addOne_tv);
            viewHolder.iv_vip = convertView.findViewById(R.id.icon_vip);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (Holder) convertView.getTag();
        }


        if (!mPinglunListData.get(position).getPublish_user().getAvatar().equals("")) {
            PicassoUtils.loadImageView(mContext, mPinglunListData.get(position).getPublish_user().getAvatar(), viewHolder.iv_head);
        }


        ImageLoader.getInstance().displayImage(mPinglunListData.get(position).getPublish_user().getAvatar(), viewHolder.iv_head);
        viewHolder.iv_head.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                //跳转到用户详情界面

                Intent intent = new Intent(mContext, DynamicDetailsActivity_new.class);
                intent.putExtra("user_id", mPinglunListData.get(position).getPublish_user().getUser_id() + "");
//                mContext.startActivity(intent);
            }
        });


        viewHolder.tv_title.setText(mPinglunListData.get(position).getPublish_user().getUsername());
        viewHolder.tv_time.setText(mPinglunListData.get(position).getUpdated_at());
        viewHolder.tv_content.setText(mPinglunListData.get(position).getContent());
        final Holder finalViewHolder = viewHolder;


//        System.out.println(mPinglunListData.get(position).getIs_like());
//
        if (mPinglunListData.get(position).getIs_like() == 1) {
            viewHolder.iv_dianzhan.setSelected(true);
        } else {
            viewHolder.iv_dianzhan.setSelected(false);
            viewHolder.iv_dianzhan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    dianzhan(position, finalViewHolder.iv_dianzhan, finalViewHolder.addOne);
                }
            });
        }


        if (mPinglunListData.get(position).getPublish_user().getIs_authenticated() == 1) {
            viewHolder.iv_vip.setVisibility(View.VISIBLE);
        } else {
            viewHolder.iv_vip.setVisibility(View.GONE);
        }


//        System.out.println(viewHolder.tv_time+"---"+viewHolder.tv_title);

        return convertView;
    }


    /**
     * 点赞
     */
    void dianzhan(final int position, final ImageView iv_dianzhan, final TextView addone) {

        System.out.println("点击的id是:" + mPinglunListData.get(position).getComment_id());

        //post请求简洁版实现
        HttpParams params = new HttpParams();
        params.putHeaders("Authorization", "Bearer " + ShareUtils.getString(mContext, AppConst.TOKEN, ""));
        params.put("resource_id", mPinglunListData.get(position).getComment_id());
        params.put("resource_type", 3);


        RxVolley.post(StaticClass.DianZan, params, new HttpCallback() {
            @Override
            public void onSuccess(String t) {
                DianZhanModel dianZhanModel = GsonUtils.GsonToBean(t, DianZhanModel.class);

                if (dianZhanModel.getCode() == AppConst.TOKENERROR) {
                    mContext.startActivity(new Intent(mContext,LoginActivity.class));
                    return;
                }

                if (dianZhanModel.getCode() == 200) {
//                    CustomToast.INSTANCE.showToast(dianZhanModel.getStatus()+dianZhanModel.getCode());
                    if (dianZhanModel.getResult().getIs_like() == 1) {
                        //发送广播 更新数据
//                        Intent intent = new Intent("android.intent.action.MY_BROADCAST");
//                        intent.putExtra("msg", "hello receiver.");
//                        mContext.sendBroadcast(intent);
                        //点赞动画效果
//                        android.view.animation.Animation animation = AnimationUtils.loadAnimation(mContext, R.anim.add_score_anim);
//                        addone.setVisibility(View.VISIBLE);
//                        addone.startAnimation(animation);
//                        new Handler().postDelayed(new Runnable() {
//                            public void run() {
//                                addone.setVisibility(View.GONE);
//                            }
//                        }, 1000);

                        iv_dianzhan.setSelected(true);
                    } else if (dianZhanModel.getResult().getIs_like() == 0) {
                        iv_dianzhan.setSelected(false);
                    }
                } else {
                    CustomToast.INSTANCE.showToast("点赞失败：" + dianZhanModel.getCode());
                }
            }
        });
    }

    class Holder {
        ImageView iv_head;
        TextView tv_title;
        TextView tv_time;
        TextView tv_content;
        ImageView iv_dianzhan;
        TextView addOne;// +1 点赞
        ImageView iv_vip;//是否是VIP
    }
}

